import axios from "axios";
import { Message } from "@garco-design/web-vue";
import { getToken, getEnv } from "@/utils/auth";
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 60000,
  headers: {
    'x-auth-enable':'false'
  }
});
// 请求拦截器
service.interceptors.request.use(
  (config) => {
    const token = getToken() || localStorage.getItem('token');
    // if (token) {
    //   config.headers.Authorization = `Bearer ${token}`;
    //   config.headers.env = `${getEnv()}`
    //   // config.baseURL = config.baseURL + `/${getEnv()}`;
    // }
    // console.log(localStorage.getItem('token'), 'config');
    config.headers['x-auth-token'] = token
    // config.headers['x-auth-enable']=false
    config.headers['X-Occupant'] = 'default';
    config.headers['amp-app-code'] = 'monitor'
    config.headers['amp-organ-id'] = '2498172061581504512'
    // 判断参数是否为空
    if (config.method === "get") {
      const query = config.params;
      for (const key in query) {
        if (!query[key]) {
          delete query[key];
        }
      }
    }
    // config.headers['x-auth-enable']=false
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);
// 响应拦截器
service.interceptors.response.use(
  function (response) {
    // console.log(response, 'res');

    const res = response.data;
    if (res.code !== 200&&res.code !== 20000) Message.error(res.message);
    return res;
  },
  async function (error) {
    // 对响应错误做点什么
    if (error.response) {
      Message.error(error.message);
    }
    return Promise.reject(error);
  }
);
export default service;
